<?php
namespace Model;

class ObjectHandler {

	private $database = NULL;
	const OBJECT_EMPTY = 0;
	const OBJECT_SUCCESS = 1;
	const OBJECT_FAILED = 2;
	const OBJECT_SAVED = 3;

	public function __construct($a_database) {
		$this -> database = $a_database;
	}

	//Visar alla object
	public function getAllobjects() {

		$sql = "SELECT o.*, c.CategoryName
				FROM object o 
				INNER JOIN category c 
				ON c.Cat_Id = o.Cat_Id";
		$stmt = $this -> database -> SqlStatement($sql);
		$stmt -> execute();
		if ($stmt -> bind_result($objID, $userId, $catId, $title, $desc, $price, $date, $catName) == FALSE) {
			throw new \Exception($this -> m_mysqli -> error);
		}
		
		//lägger dom i en array
		$ret = new ObjectArray();
		while ($stmt -> fetch()) {
			$object = new Object($objID, $userId, $catId, $catName, $title, $desc, $price, $date);
			$ret -> add($object);
		}

		$stmt -> close();
		return $ret;
	}
	
	//Hämtar vald object
	public function getObjByID($objectID) {
		
		$sql = "SELECT o.*, c.CategoryName
				FROM object o 
				INNER JOIN category c 
				ON c.Cat_Id = o.Cat_Id
				WHERE Obj_Id = ?";
				
		$stmt = $this -> database -> SqlStatement($sql);
		$stmt -> bind_param("i", $objectID);

		$stmt -> execute();
		if ($stmt -> bind_result($objectID,$userID,$catId, $title, $desc, $price,$date, $catName) == FALSE) {
			throw new \Exception($this -> m_mysqli -> error);			
		}
		
		if ($stmt -> fetch()) {
			
			$ret = new Object($objectID,$userID,$catId, $catName, $title, $desc, $price,$date);
			
		} else {
			throw new \Exception("hittar inte object med id=" . $objectID);
		}
		$stmt -> close();
		
		
		return $ret;
	}

	//Skapar nytt object
	public function newObject($userId, $Cat_Id ,$ObjTitle, $ObjDesc, $ObjPrice) {
		$validate = new \Common\Validator();
		if (isset($userId) && isset($Cat_Id)&& isset($ObjTitle)&& isset($ObjDesc)&& isset($ObjPrice)) {
			$title1 = $validate -> filterHTML($ObjTitle);
			$Desc = $validate -> filterHTML($ObjDesc);
			$Price = $validate -> filterHTML($ObjPrice);
			$sql = "INSERT INTO object (User_Id, Cat_Id,ObjTitle, ObjDesc,ObjPrice) VALUES (?, ?, ?, ?, ?)";

			if ($ObjTitle == '' || $ObjDesc == '') {
				return self::OBJECT_EMPTY;
			}

			if ($stmt = $this -> database -> SqlStatement($sql)) {
				$stmt -> bind_param('iissi',$userId, $Cat_Id, $title1, $Desc,$Price);

				$stmt -> execute();

				$stmt -> close();

				return self::OBJECT_SUCCESS;
			}
		}
		return self::OBJECT_FAILED;
	}
	/*
	//Redigera inlägg
	public function EditPost($p_id, $title, $content) {

		if (isset($title) && isset($content)) {
			$sql = "UPDATE post SET title = ?, content = ? WHERE p_id = ? ";

			if ($title == '' || $content == '') {
				return self::OBJECT_EMPTY;
			}
			if ($stmt = $this -> database -> SqlStatement($sql)) {
				$stmt -> bind_param('ssi', $title, $content, $p_id);

				$stmt -> execute();

				$stmt -> close();

				return self::OBJECT_SAVED;
			}
		}
		return self::OBJECT_FAILED;
	}

	//Radera inlägg
	public function DeletePost($p_id) {

		if (isset($p_id)) {
			$sql = "DELETE FROM post WHERE p_id = ?";

			if ($stmt = $this -> database -> SqlStatement($sql)) {

				$stmt -> bind_param('i', $p_id);

				$stmt -> execute();

				$stmt -> close();
				return true;
			}
		}

		return false;
	}
	 * 
	 */
}
